package cn.lzm.server.tcp;

import io.vertx.core.Vertx;
import io.vertx.core.net.NetSocket;

/**
 * @author lzm
 * @Date 2024/11/5
 * @description 测试tcp的半包和粘包
 */
public class VertxTcpClientV2 {

    public void start() {
        Vertx vertx = Vertx.vertx();
        vertx.createNetClient().connect(8404, "localhost", result -> {
            if (result.succeeded()) {
                System.out.println("connected to server");
                NetSocket socket = result.result();
                for (int i = 0; i < 1000; i++) {
                    socket.write("hello, server! hello, server! hello, server! hello, server!");
                }
                socket.handler(buffer -> {
                    System.out.println("received response from server: " + buffer.toString());
                });
            } else {
                System.err.println("failed to connect to tcp server");
            }
        });
    }

    public static void main(String[] args) {
        new VertxTcpClientV2().start();
    }
}
